﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace JobDescOnline.Presentation.Models
{
    public static class Competencies
    {
        public static JobDescDBEntities1 db = new JobDescDBEntities1();
        public static dynamic RetrieveByFilterAndPage(List<FilterData> filters, int page, int pagesize, string sort, string direction)
        {
            var _temp = from c in db.Competencies
                        select new CompetencyTempModel
                        {
                            ID = c.ID,
                            Rowstatus = c.Rowstatus,
                            Description = c.Description,
                            Definition = c.Definition,
                            CreatedBy = c.CreatedBy,
                            CreatedOn = c.CreatedOn
                        };
            
            if (direction.ToLower() == "asc")
            {
                switch (sort)
                {
                    case "ID": _temp = _temp.OrderBy(s => s.ID); break;
                    case "Description": _temp = _temp.OrderBy(s => s.Description); break;
                }
            }
            else
            {
                switch (sort)
                {
                    case "ID": _temp = _temp.OrderByDescending(s => s.ID); break;
                    case "Description": _temp = _temp.OrderByDescending(s => s.Description); break;
                }
            }
            
            if (filters.Count > 0)
            {
                foreach (FilterData filter in filters)
                {
                    switch (filter.column)
                    {
                        case "Description": _temp = _temp.Where(s => s.Description.Contains(filter.value)); break;
                    }
                }
            }

            return _temp.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }

        public static dynamic RetrieveByFilterAndPage(int page, int pagesize, string sort, string direction)
        {
            var _temp = from c in db.Competencies
                        select new CompetencyTempModel
                        {
                            ID = c.ID,
                            Rowstatus = c.Rowstatus,
                            Description = c.Description,
                            Definition = c.Definition,
                            CreatedBy = c.CreatedBy,
                            CreatedOn = c.CreatedOn
                        };

            if (direction.ToLower() == "asc")
            {
                switch (sort)
                {
                    case "ID": _temp = _temp.OrderBy(s => s.ID); break;
                    case "Description": _temp = _temp.OrderBy(s => s.Description); break;
                }
            }
            else
            {
                switch (sort)
                {
                    case "ID": _temp = _temp.OrderByDescending(s => s.ID); break;
                    case "Description": _temp = _temp.OrderByDescending(s => s.Description); break;
                }
            }

            return _temp.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }

        public static int RetrieveByFilter(List<FilterData> filters)
        {
            var _temp = from c in db.Competencies
                        select new CompetencyTempModel
                        {
                            ID = c.ID,
                            Rowstatus = c.Rowstatus,
                            Description = c.Description,
                            Definition = c.Definition,
                            CreatedBy = c.CreatedBy,
                            CreatedOn = c.CreatedOn
                        };

            if (filters.Count > 0)
            {
                foreach (FilterData filter in filters)
                {
                    switch (filter.column)
                    {
                        case "Description": _temp = _temp.Where(s => s.Description.Contains(filter.value)); break;
                    }
                }
            }

            return _temp.ToList().Count;
        }

        public static int RetrieveByFilter()
        {
            var list = db.Competencies.ToList();
            return list.Count;
        }

        public static SelectList CompetencyID
        {
            get
            {
                return new SelectList(db.Competencies.Where(d => d.Rowstatus == true), "ID", "Description");
            }
        }
    }

    public class CompetencyTempModel
    {
        public int ID { get; set; }
        public bool Rowstatus { get; set; }
        public string Description { get; set; }
        public string Definition { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<DateTime> CreatedOn { get; set; }
        public string LastModifiedBy { get; set; }
        public Nullable<DateTime> LastModifiedOn { get; set; }
    }

    public class CompetencyModel
    {
        public Competency Competency { get; set; }
        public IEnumerable<LevelCompetency> LevelCompetencies { get; set; }
    }
}